package net.minecraft.src;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class InscriptionManager {

	public static boolean checkInscription(int x, int y, int z, World world, EntityPlayer ep) {
		if (inscriptions == null || inscriptions.size() == 0) {
			mod_Inscription.debug("No inscriptions added!");
			return false;
		}
//		ep.addChatMessage("Checking inscriptions.");
		Iterator iter = inscriptions.iterator();
		
		prevBlockX = x;
		prevBlockY = y;
		prevBlockZ = z;
		
		int startBlock = world.getBlockId(x, y, z);
//		ep.addChatMessage("Check block is " + startBlock);
		if(startBlock < mod_Inscription.chalkDustRed.blockID || startBlock > mod_Inscription.chalkDustBlack.blockID){
			y++;
			prevBlockY++;
		}
		while (iter.hasNext()) {
			Inscription inscription = (Inscription)iter.next();

			int offset = (inscription.size-1)/2;
			int ix = x - offset;
			int iz = z + offset;
			int px = 0;
			int pz = 0;
			
			boolean matches = true;
			while (ix <= x + offset) {
				while (iz >= z - offset) {
					int worldBlock = world.getBlockId(ix, y, iz);
					int patternBlock = (Integer)mapping.get(inscription.pattern[px][pz]);
					if (worldBlock != patternBlock) {
						matches = false;
					}
					if(patternBlock < 0) matches = true;
					pz++;
					iz--;
				}
				ix++;
				iz = z + offset;
				px++;
				pz = 0;
			}
			
			if (matches) {
				mod_Inscription.debug("This is: " + inscription.name);
				Inscription.doInscription(inscription.command, ep, world);
				return true;
			}
			else {
//				mod_Inscription.debug("Not: " + inscription.name);
			}
		}

		
		return false;
	}
	
	//Green: Giving life
//	public static Inscription tree = new Inscription(7, "Tree Inscription", "growTree", 0,4,0,0,4,0,0,
//																						4,4,0,0,4,0,4,
//																						0,0,4,4,4,4,0,
//																						0,0,4,0,4,0,0,
//																						4,4,4,4,4,0,0,
//																						0,4,0,0,0,4,0,
//																						4,0,4,0,4,0,4);
	public static Inscription tree = new Inscription(7, "Tree Inscription", "growTree", 0,4,0,0,0,4,0,
																						4,0,4,4,4,0,4,
																						4,0,4,4,4,0,4,
																						0,0,4,0,4,0,0,
																						0,0,0,4,0,0,0,
																						0,0,4,4,4,0,0,
																						0,4,4,4,4,4,0);
	
	public static Inscription flower = new Inscription(3, "Flower Inscription", "growFlower", 4,0,4,
																							  0,4,0,
																							  4,0,4);
	
	//Red: Destruction Inscriptions
	public static Inscription fireball = new Inscription(5, "Fireball Inscription", "enchantFireball", 1,0,0,0,1,
																									   1,0,0,1,0,
																									   1,1,1,0,0,
																									   1,0,1,0,0,
																									   1,1,1,1,1);
	public static Inscription lightning = new Inscription(5, "Lightning Inscription", "enchantLightning", 0,0,0,1,0,
			   																							  0,0,1,0,0,
			   																							  0,1,1,1,0,
			   																							  0,0,1,0,0,
			   																							  0,1,0,0,0);
	public static Inscription test1 = new Inscription(3, "Test1 Inscription", "test1Inscrip", 1,1,1,
																						      1,0,1,
																						      1,1,1);
	public static Inscription test2 = new Inscription(3, "Test2 Inscription", "test2Inscrip", 0,0,0,
																							  0,1,0,
																							  0,0,0);
	public static Inscription bulkMiner = new Inscription(5, "Bulk Miner", "bulkMiner", 0,1,1,1,0,
																						1,0,1,0,1,
																						0,0,1,0,0,
																						0,0,1,0,0,
																						1,0,1,0,1);
	
	//Blue: Building Inscriptions
	public static Inscription bridgeNorth = new Inscription(5, "Bridge N Inscription", "bridgeNorth", 0,0,0,0,0,
																									  0,0,0,0,0,
																									  3,3,3,3,3,
																									  3,0,0,0,3,
																									  3,0,0,0,3);
	public static Inscription bridgeSouth = new Inscription(5, "Bridge S Inscription", "bridgeSouth", 3,0,0,0,3,
																									  3,0,0,0,3,
																									  3,3,3,3,3,
																									  0,0,0,0,0,
																									  0,0,0,0,0);
	public static Inscription bridgeEast = new Inscription(5, "Bridge E Inscription", "bridgeEast", 3,3,3,0,0,
																								    0,0,3,0,0,
																								    0,0,3,0,0,
																								    0,0,3,0,0,
																								    3,3,3,0,0);
	public static Inscription bridgeWest = new Inscription(5, "Bridge W Inscription", "bridgeWest", 0,0,3,3,3,
																								    0,0,3,0,0,
																								    0,0,3,0,0,
																								    0,0,3,0,0,
																								    0,0,3,3,3);
	public static Inscription tunnelNorth = new Inscription(3, "Tunnel N Inscription", "tunnelNorth", 3,3,3,
																									  3,0,3,
																									  0,0,0);
	public static Inscription tunnelSouth = new Inscription(3, "Tunnel S Inscription", "tunnelSouth", 0,0,0,
																									  3,0,3,
																									  3,3,3);
	public static Inscription tunnelEast = new Inscription(3, "Tunnel E Inscription", "tunnelEast", 0,3,3,
																									0,0,3,
																									0,3,3);
	public static Inscription tunnelWest = new Inscription(3, "Tunnel W Inscription", "tunnelWest", 3,3,0,
																									3,0,0,
																									3,3,0);
	public static Inscription stairsDown = new Inscription(7, "Staircase D Inscription", "stairsDown", 0,0,0,0,3,0,0,
																									   0,0,0,0,3,0,0,
																									   0,0,3,0,3,0,3,
																									   0,0,0,3,3,3,0,
																									   3,0,0,0,3,0,0,
																									   3,3,0,0,0,0,0,
																									   3,3,3,0,0,0,0);
	public static Inscription shack = new Inscription(5, "Shack Inscription", "buildShack", 3,3,3,3,3,
																							3,0,0,0,3,
																							3,0,0,0,3,
																							3,0,0,0,3,
																							3,3,0,3,3);
	public static Inscription hut = new Inscription(9, "Hut Inscription", "buildHut", 0,0,0,3,3,3,0,0,0,
																					  0,0,3,0,0,0,3,0,0,
																					  0,3,0,0,0,0,0,3,0,
																					  3,0,0,0,0,0,0,0,3,
																					  3,0,0,0,0,0,0,0,3,
																					  3,0,0,0,0,0,0,0,3,
																					  0,3,0,0,0,0,0,3,0,
																					  0,0,3,0,0,0,3,0,0,
																					  0,0,0,3,0,3,0,0,0);
	public static Inscription house = new Inscription(11, "House Inscription", "buildHouse", 3,3,3,3,3,3,3,3,3,3,3,
																							 3,0,0,0,0,0,0,0,0,0,3,
																							 3,0,0,0,0,0,0,0,0,0,3,
																							 3,0,0,0,0,0,0,0,0,0,3,
																							 3,0,0,0,0,0,0,0,0,0,3,
																							 3,0,0,0,0,0,0,0,0,0,3,
																							 3,0,0,0,0,0,0,0,0,0,3,
																							 3,0,0,0,0,0,0,0,0,0,3,
																							 3,0,0,0,0,0,0,0,0,0,3,
																							 3,0,0,0,0,0,0,0,0,0,3,
																							 3,3,3,3,3,0,3,3,3,3,3); //LOLOLOLOLOL
	
	//Black: Teleporting
	public static Inscription nodeS = new Inscription(5, "Node S Inscription", "senderNode", 0,5,5,5,0,
																						     5,0,5,0,5,
																						     5,5,5,5,5,
																						     5,0,5,0,5,
																						     0,5,5,5,0);
	public static Inscription nodeR = new Inscription(5, "Node R Inscription", "receiverNode", 0,5,5,5,0,
																							   5,5,0,5,5,
																							   5,0,5,0,5,
																							   5,5,0,5,5,
																							   0,5,5,5,0);
	public static Inscription mark = new Inscription(3, "Mark Inscription", "marker", 5,0,5,
																					  0,5,0,
																					  5,0,5);
	public static Inscription recall = new Inscription(3, "Recall Inscription", "recall", 0,5,0,
																						  5,5,5,
																						  0,5,0);
	public static Inscription home = new Inscription(3, "Home Inscription", "home", 5,5,5,
																					5,0,5,
																					5,5,5);
	
	//Gold: Conjuring Items
	public static Inscription cake = new Inscription(3,"Cake Inscription", "giveCake", 2,2,2,
																					   2,0,2,
																					   2,2,2);
	public static Inscription rails = new Inscription(5,"Rail Inscription", "giveRail", 2,0,2,0,2,
																						0,2,0,2,0,
																						0,2,0,2,0,
																						0,2,0,2,0,
																						2,0,2,0,2);
	public static Inscription shears = new Inscription(5, "Shears Inscription", "giveShears", 0,0,2,0,0,
																							  0,2,0,0,0,
																							  2,0,0,0,2,
																							  2,0,0,2,0,
																							  2,2,2,0,0);
	public static Inscription direction = new Inscription(3 , "Direction Inscription", "giveDirection", 0,2,0,
																										2,2,2,
																										0,2,0);
	
	public static int prevBlockX;
	public static int prevBlockY;
	public static int prevBlockZ;
	
	public static ArrayList<Inscription> inscriptions = new ArrayList<Inscription>();
	public enum materials {ANY,AIR,REDDUST,GOLDDUST,BLUEDUST,GREENDUST,BLACKDUST};
	public static Map mapping = new HashMap<Integer,Integer>();
	
	
	
	static {
		//destruction
		inscriptions.add(fireball);
		inscriptions.add(lightning);
		inscriptions.add(test1);
		inscriptions.add(test2);
		inscriptions.add(bulkMiner);
		//conjure
		inscriptions.add(cake);
		inscriptions.add(rails);
		inscriptions.add(shears);
		inscriptions.add(direction);
		//build
		inscriptions.add(tunnelNorth);
		inscriptions.add(tunnelSouth);
		inscriptions.add(tunnelEast);
		inscriptions.add(tunnelWest);
		inscriptions.add(bridgeNorth);
		inscriptions.add(bridgeSouth);
		inscriptions.add(bridgeEast);
		inscriptions.add(bridgeWest);
		inscriptions.add(stairsDown);
		inscriptions.add(shack);
		inscriptions.add(hut);
		inscriptions.add(house);
		//growth
		inscriptions.add(tree);
		inscriptions.add(flower);
		//teleport
		inscriptions.add(nodeS);
		inscriptions.add(nodeR);
		inscriptions.add(mark);
		inscriptions.add(recall);
		inscriptions.add(home);
		//inscription materials
		mapping.put(-1, -1);
		mapping.put(0, 0);
		mapping.put(1, mod_Inscription.chalkDustRed.blockID);
		mapping.put(2, mod_Inscription.chalkDustGold.blockID);
		mapping.put(3, mod_Inscription.chalkDustBlue.blockID);
		mapping.put(4, mod_Inscription.chalkDustGreen.blockID);
		mapping.put(5, mod_Inscription.chalkDustBlack.blockID);
		
	};
}
